package com.wxdc.nmgyt.nmgytwydc.greendao.manager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.wxdc.nmgyt.nmgytwydc.greendao.DaoMaster;
import com.wxdc.nmgyt.nmgytwydc.greendao.DaoSession;
import com.wxdc.nmgyt.nmgytwydc.greendao.helper.MyOpenHelper;

/**
 * Created by Administrator on 2018/4/27.
 * 数据库管理类
 */

public class DbManager {
    private static final String DB_NAME = "wycj.db";
    private static DbManager sDbManager;
    private static DaoMaster.DevOpenHelper sDevOpenHelper;
    private static DaoMaster sDaoMaster;
    private static DaoSession sDaoSession;
    private Context mContext;

    private DbManager(Context context) {
        mContext = context;
        //初始化数据库信息
        sDevOpenHelper = new DaoMaster.DevOpenHelper(context, DB_NAME);
    }

    public static DbManager getDbManager(Context context) {
        if (null == sDbManager) {
            synchronized (DbManager.class) {
                if (null == sDbManager) {
                    sDbManager = new DbManager(context);
                }
            }
        }
        return sDbManager;
    }

    /**
     * 获得可读数据库(当数据库不可写入的时候（如磁盘空间已满）getReadableDatabase()方法返回的对象将以只读的方式去打开数据库)
     *
     * @param context
     * @return
     */
    public static SQLiteDatabase getReadableDatabase(Context context) {
        if (null == sDevOpenHelper) {
            getDbManager(context);
        }
        return sDevOpenHelper.getReadableDatabase();
    }

    /**
     * 获得可写数据库(当数据库不可写入的时候（如磁盘空间已满）getWritableDatabase()方法则将出现异常)
     *
     * @param context
     * @return
     */
    public static SQLiteDatabase getWriteableDatabase(Context context) {
        if (null == sDevOpenHelper) {
            getDbManager(context);
        }
        return sDevOpenHelper.getWritableDatabase();
    }

    /**
     * 获取DaoMaster
     *
     * @param context
     * @return
     */
   /* public static DaoMaster getDaoMaster(Context context) {
        synchronized (DaoMaster.class) {
            if (null == sDaoMaster) {
                sDaoMaster = new DaoMaster(getWriteableDatabase(context));
            }
        }
        return sDaoMaster;
    }*/
    /**
     * 获取DaoMaster
     *
     * 判断是否存在数据库，如果没有则创建数据库
     * @param context
     * @return
     */
    public static DaoMaster getDaoMaster(Context context) {
        if (null == sDaoMaster) {
            synchronized (DaoMaster.class) {
                if (null == sDaoMaster) {
                    MyOpenHelper helper = new MyOpenHelper(context, DB_NAME, null);
                    sDaoMaster = new DaoMaster(helper.getWritableDb());
                }
            }
        }
        return sDaoMaster;
    }

    /**
     * 获取DaoSession
     *
     * @param context
     * @return
     */
    public static DaoSession getDaoSession(Context context) {
        if (null == sDaoSession) {
            synchronized (DaoSession.class) {
                if (null == sDaoSession) {
                    sDaoSession = getDaoMaster(context).newSession();
                }
            }
        }
        return sDaoSession;
    }
}
